﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;

namespace ZLDC.Common.Estate
{
    public class _Json
    {
        public DataTable dt;

        public string DateFormat = "yyyy-MM-dd";

        public bool IsUBB = false;

        public int IsDateChange = 0;

        public _Json(DataTable dt)
        {
            this.dt = dt;
        }

        public _Json(DataTable dt, int _IsDateChange)
        {
            this.dt = dt;
            this.IsDateChange = _IsDateChange;
        }

        public _Json(DataTable dt, bool IsUBB)
        {
            this.dt = dt;
            this.IsUBB = IsUBB;
        }

        public _Json(DataTable dt, string DateFormat)
        {
            this.dt = dt;
            this.DateFormat = DateFormat;
        }

        public _Json(DataTable dt, bool IsUBB, string DateFormat)
        {
            this.dt = dt;
            this.IsUBB = IsUBB;
            this.DateFormat = DateFormat;
        }

        public string ToJson()
        {
            if (dt == null)
                return "[]";

            string colname = ""; string value = ""; StringBuilder jsonstr = new StringBuilder();
            jsonstr.Append("[");

            int rows = dt.Rows.Count; int columns = dt.Columns.Count;
            for (int i = 0; i < rows; i++)
            {
                jsonstr.Append("{");
                for (int j = 0; j < columns; j++)
                {
                    colname = dt.Columns[j].ColumnName.ToLower();
                    value = dt.Rows[i][j].ToString().Replace("'", "&apos;").Replace("\"", "").Replace("\n", "").Replace("\r", "").Replace("\0", "").Replace("\\", "\\\\");

                    if (this.IsDateChange != 1)
                    {
                        if (dt.Columns[j].DataType.Name.ToLower() == "datetime")
                            value = _Convert.ToDateTime(value).ToString(DateFormat);
                    }
                    if (dt.Columns[j].DataType.Name.ToLower() == "string")
                        value = HttpUtility.HtmlEncode(HttpUtility.HtmlDecode(value));

                    jsonstr.Append(colname + ":'" + value + "'");
                    if (j < dt.Columns.Count - 1)
                        jsonstr.Append(",");
                }
                jsonstr.Append("}");

                if (i < dt.Rows.Count - 1)
                    jsonstr.Append(",");
            }
            jsonstr.Append("]");

            return jsonstr.ToString();
        }

        public string ToJson(string itemvalue)
        {
            if (dt == null)
                return "[]";

            string colname = ""; string value = ""; StringBuilder jsonstr = new StringBuilder();
            jsonstr.Append("[");

            int rows = dt.Rows.Count; int columns = dt.Columns.Count;
            for (int i = 0; i < rows; i++)
            {
                if (itemvalue.Contains(dt.Rows[i]["item"].ToString()))
                    continue;

                jsonstr.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    colname = dt.Columns[j].ColumnName.ToLower();
                    value = dt.Rows[i][j].ToString().Replace("'", "&apos;").Replace("\"", "").Replace("\n", "").Replace("\r", "").Replace("\0", "").Replace("\\", "\\\\");

                    if (this.IsDateChange != 1)
                    {
                        if (dt.Columns[j].DataType.Name.ToLower() == "datetime")
                            value = _Convert.ToDateTime(value).ToString(DateFormat);
                    }
                    if (dt.Columns[j].DataType.Name.ToLower() == "string")
                        value = HttpUtility.HtmlEncode(HttpUtility.HtmlDecode(value));

                    jsonstr.Append(colname + ":'" + value + "'");
                    if (j < dt.Columns.Count - 1)
                        jsonstr.Append(",");
                }
                jsonstr.Append("}");
                if (i < dt.Rows.Count - 1)
                    jsonstr.Append(",");
            }
            jsonstr.Append("]");

            return jsonstr.ToString();
        }

    }
}
